/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vehicle.bol;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import vehicle.dal.DAL;
import vehicle.entity.Employee;

/**
 *
 * @author lab1
 */
public class EmployeeBOL {

    public static Employee checkLogin(String account, String password) {
        DAL dal = new DAL();
        try {
            ArrayList paramlist = new ArrayList();
            paramlist.add(account);
            // paramlist.add(password);
            paramlist.add(MD5.getHash(String.valueOf(password)));
            ResultSet rs = dal.executeQuery("loginAdmin", paramlist);
            if (rs.next()) {
                Employee e = new Employee();
                e.setUserName(rs.getString("UserName"));
                e.setPassword(rs.getString("Password"));
                e.setFullName(rs.getString("FullName"));
                e.setPhone(rs.getString("Phone"));
                e.setEmail(rs.getString("Email"));
                e.setAddress(rs.getString("Address"));
                e.setBrandID(rs.getInt("BrandID"));
                e.setPosition(rs.getInt("position"));
                e.setStatus(rs.getBoolean("STATUS"));
                return e;
            }
        } catch (Exception ex) {
            ex.getMessage();
        } finally {
            dal.closeConnection();
        }
        return null;
    }

    public static Employee checkEmployee(String account) {
        DAL dal = new DAL();
        try {
            ArrayList paramlist = new ArrayList();
            paramlist.add(account);
            ResultSet rs = dal.executeQuery("checkEmployee", paramlist);
            if (rs.next()) {
                Employee e = new Employee();
                e.setUserName(rs.getString("UserName"));
                e.setPassword(rs.getString("Password"));
                e.setFullName(rs.getString("FullName"));
                e.setPhone(rs.getString("Phone"));
                e.setEmail(rs.getString("Email"));
                e.setAddress(rs.getString("Address"));
                e.setBrandID(rs.getInt("BrandID"));
                e.setPosition(rs.getInt("position"));
                e.setStatus(rs.getBoolean("STATUS"));
                return e;
            }
        } catch (Exception ex) {
            ex.getMessage();
        } finally {
            dal.closeConnection();
        }
        return null;
    }

    public List<Employee> showAllEmp() {
        DAL dal = new DAL();
        List<Employee> listem = new ArrayList<Employee>();
        try {
            ResultSet rs = dal.executeQuery("getAllEmployee", new ArrayList());
            while (rs.next()) {
                Employee em = new Employee();
                em.setUserName(rs.getString("UserName"));
                em.setPassword(rs.getString("Password"));
                em.setFullName(rs.getString("FullName"));
                em.setPhone(rs.getString("Phone"));
                em.setEmail(rs.getString("Email"));
                em.setAddress(rs.getString("Address"));
                em.setBrandID(rs.getInt("BrandID"));
                em.setPosition(rs.getInt("position"));
                listem.add(em);
            }
        } catch (Exception e) {
        } finally {
            dal.closeConnection();
        }
        return listem;
    }

    public static int selectBrandIDByBrandName(String brandName) {
        DAL dal = new DAL();
        int chek = 0;
        try {
            ArrayList arrbramID = new ArrayList();
            arrbramID.add(brandName);
            ResultSet rs = dal.executeQuery("selectBrandIDByBrandName", arrbramID);
            if (rs.next()) {
                chek = rs.getInt("BrandID");
                return chek;
            }
        } catch (Exception e) {
            e.getMessage();
        } finally {
            dal.closeConnection();
        }
        return chek;
    }

    public static int insertEmployee(Employee em, String brandName) {
        DAL dal = new DAL();
        int chek = 0;
        int branID = EmployeeBOL.selectBrandIDByBrandName(brandName);
        try {
            ArrayList arr = new ArrayList();
            arr.add(em.getUserName());
            // arr.add(em.getPassword());
            arr.add(MD5.getHash(String.valueOf(em.getPassword())));
            arr.add(em.getFullName());
            arr.add(em.getPhone());
            arr.add(em.getEmail());
            arr.add(em.getAddress());
            arr.add(branID);
            arr.add(em.getPosition());
            chek = dal.executeUpdateQuery("addEmployee", arr);
        } catch (Exception e) {
            e.getMessage();
        } finally {
            dal.closeConnection();
        }
        return chek;
    }

    public int deleteEmployee(String UserName) {
        DAL dal = new DAL();
        int check = 0;
        try {
            ArrayList arrlist = new ArrayList();
            arrlist.add(UserName);
            check = dal.executeUpdateQuery("deleteEmployee", arrlist);
        } catch (Exception ex) {
        } finally {
            dal.closeConnection();
        }
        return check;
    }

    public static int eidtEmployee(Employee em,String brandName) {
        DAL dal = new DAL();
        int chek = 0;
        int branID = EmployeeBOL.selectBrandIDByBrandName(brandName);
        try {
            ArrayList arr = new ArrayList();
            arr.add(em.getUserName());
            arr.add(em.getFullName());
            arr.add(em.getPhone());
            arr.add(em.getEmail());
            arr.add(em.getAddress());
            arr.add(branID);
            arr.add(em.getPosition());
            chek = dal.executeUpdateQuery("editUpdateEmployee", arr);
        } catch (Exception e) {
            e.getMessage();
        } finally {
            dal.closeConnection();
        }
        return chek;
    }

    public static int changepassword(String username, String newpassword) {
        DAL dal = new DAL();
        int chek = 0;
        try {
            ArrayList arr = new ArrayList();
            arr.add(username);
            arr.add(MD5.getHash(String.valueOf(newpassword)));
            chek = dal.executeUpdateQuery("changpassword", arr);
        } catch (Exception e) {
            e.getMessage();
        } finally {
            dal.closeConnection();
        }
        return chek;

    }
}
